package de.breitbeil.jbooking.ui.view;

import java.io.IOException;

import org.springframework.binding.value.ValueModel;
import org.springframework.richclient.application.PageComponentContext;
import org.springframework.richclient.command.support.AbstractActionCommandExecutor;
import org.springframework.richclient.list.ListSelectionValueModelAdapter;
import org.springframework.richclient.list.ListSingleSelectionGuard;

import de.breitbeil.jbooking.domain.Person;
import de.breitbeil.jbooking.ui.dataview.DataView;
import de.breitbeil.jbooking.ui.dataview.DataViewTable;
import de.breitbeil.jbooking.ui.dataview.columns.DataViewColumnImpl;
import de.breitbeil.jbooking.ui.dataview.columns.StringPropertyDataViewColumn;
import de.breitbeil.jbooking.ui.form.PersonForm;
import de.breitbeil.jbooking.ui.view.tablecellrenderer.ConcatenateTableCellRenderer;

public class PersonView extends DataView {

    public final static String PAGE_ID = "personView";
    
    private DarsyExecutor darsyExecutor = new DarsyExecutor();
    
    public PersonView() throws InstantiationException, IllegalAccessException {
        super(PAGE_ID, Person.class, PersonForm.class);
    }
    
    public void init() {
        addColumn(new StringPropertyDataViewColumn("lastname"));
        addColumn(new StringPropertyDataViewColumn("firstname"));
        addColumn(createAdressColumn());
        addColumn(new StringPropertyDataViewColumn("email"));
        
        addExportColumn("id");
        addExportColumn("firstname");
        addExportColumn("lastname");
        addExportColumn("address1");
        addExportColumn("address2");
        addExportColumn("address3");
        addExportColumn("zip");
        addExportColumn("city");
        addExportColumn("homePhone");
        addExportColumn("workPhone");
        addExportColumn("email");
        addExportColumn("description");
        addExportColumn("address0");
        addExportColumn("address");
        addExportColumn("mobil");
        addExportColumn("birthday");
        addExportColumn("p_firstname");
        addExportColumn("p_lastname");
        addExportColumn("p_birthday");
    }
    
    @Override
    protected void registerLocalCommandExecutors(PageComponentContext context) {
    	super.registerLocalCommandExecutors(context);
    	
    	context.register("darsyCommand", darsyExecutor);
    }
    
    @Override
    protected DataViewTable createTablePanel() {
    	DataViewTable table = super.createTablePanel();
    	
    	// Executor nur aktivieren, wenn mindestens ein Objekt selektiert ist
    	ValueModel selectionHolder = new ListSelectionValueModelAdapter(getDataViewTable().getSelectionModel());
        new ListSingleSelectionGuard(selectionHolder, darsyExecutor);
        
        return table;
    }

    protected DataViewColumnImpl createAdressColumn() {
        String[] properties = new String[]{ "address1", "address2", "address3" };
        ConcatenateTableCellRenderer renderer = new ConcatenateTableCellRenderer(Person.class, properties, "<br>");
        renderer.addProperty("zip", " ");
        renderer.addProperty("city", "");
        
        DataViewColumnImpl column = new DataViewColumnImpl("address");
        column.setCellRenderer(renderer);
        column.setFilterProperties(new String[]{"address1", "address2", "address3", "zip", "city"} );
        column.setObjectCellRenderer(true);
        return column;
    }
    
    private class DarsyExecutor extends AbstractActionCommandExecutor {
        public void execute() {
        	Person selectedObject = (Person)getDataViewTable().getSelectedObject();
        	Long id = selectedObject.getId();
        	try {
				Runtime.getRuntime().exec("cmd /c start darsy_extern.vbs \"" + selectedObject.getLastname() + "_" +
				        selectedObject.getFirstname() + "\" " + id);
        	    
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
        }
    }

}

